import {getGoodsInfoXCX} from '../../../Tools/HTTPOpertion.js';
Page({
  data: {
    userId: '',
    icon: 'https://mpic.denong.com/xcx-c-20180703-dui.png',
    isShow: 'none',
    alertTop: 0,
    gonlList: [],
    id: 0,
    obj: {},
    types: [],
    markers: [{
      iconPath: "https://activity.denong.com/dw.png",
      id: 0,
      latitude: 23.099994,
      longitude: 113.324520,
      width: 50,
      height: 50
    }],
    allLikes: [],
    likes: [],
    currentLikes: [],
    services: [],
    allServices: [],
    minServices: [],
    pics: [],
    miaoTime:1,//秒杀，即将开始
    countDownHour:'00',
    countDownMinute: '00',
    countDownSecond: '00',
    times:'',
    miaoTime:1
  },

  showAlert: function () {
    this.setData({
      alertTop: 50,
      isShow: "block"
    });
  },
  close: function () {
    this.setData({
      alertTop: 0,
      isShow: "none"
    });
  },
  ddhua: function (e) {
    const phoneNumber = e.currentTarget.dataset.store_phone;
    wx.makePhoneCall({
      phoneNumber,
    })
  },
  shop_list_btn: function (e) {
    var id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: '/pages/others/coupon/coupon?id=' + id
    })
  },
  tpTap: function (e) {

    var id = e.currentTarget.dataset.id;

    wx.navigateTo({
      url: '/pages/others/Album/Album?id=' + id
    })
  },

  callPhone: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone
    })
  },
  oneKeyGroup1: function (e) {
    // console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
      url: '/pages/active/miaoshaOrder/miaoshaOrder?orderId=' + e.currentTarget.dataset.id
    })
  },
  oneKeyGroup2: function (e) {
    wx.showToast({
      title: '活动尚未开始',
      icon:'none',
      duration: 2000
    })
  },

  toShop: function (e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/others/MerchantDetails/MerchantDetails?id=${id}`,
    })
  },
  onShareAppMessage: function () {
    const self = this;
    return {
      title: self.data.obj.name,
      path: `/pages/active/miaoshaDetails/miaoshaDetails?id=${self.data.obj.id}`,
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '转发成功',
          duration: 2000
        })
        self.setData({ showModal: false })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '转发失败',
          duration: 2000
        })
      }
    }
  },
  backHome: function (e) {
    wx.switchTab({
      url: '/pages/main/spellGroupHome/spellGroupHome',
    })
  },
  navToMap: function (e) {
    console.log(e);
    var lat = e.currentTarget.dataset.info.latitude;
    var log = e.currentTarget.dataset.info.longitude;
    var name = e.currentTarget.dataset.info.name;
    // wx.navigateTo({
    //   url: '../map/map?lat='+lat+"&log="+log+"&name="+name
    // })
    wx.openLocation({
      latitude: parseFloat(lat),
      longitude: parseFloat(log),
      name: name,
      scale: 18
    })
  },
  showAll: function () {
    this.setData({
      currentLikes: this.data.currentLikes.length == this.data.likes.length ? this.data.allLikes : this.data.likes
    });
  },

  loadData: function () {
    var self = this;
    console.log(self.data.id);
    console.log('self.data', self.data);

      getGoodsInfoXCX({ cnd: self.data.id }).then(function (result) {
        console.log(result)
        // 我的订单获取时间
        if(self.data.times=='123'){
          console.log(11111111111111111111111111)
          var starttime = Date.parse(new Date()) / 1000;
          var endtime = result.data.end_date;
          self.theTime(endtime)
        }
        wx.hideLoading();
        let explain_img_url = result.data.explain_img_url;
        if (explain_img_url) {
          explain_img_url = JSON.parse(explain_img_url);
        } else {
          explain_img_url = "";
        }
        console.warn('create_person_id：', result.data.create_person_id, 'userId: ', self.data.userId, 'obj: ', result.data);
        self.setData({
          obj: result.data,
          'obj.imgList': explain_img_url,
          'obj.xiaojin': result.data.currency / 100
        });
        console.warn(explain_img_url);
      })
        .catch(function (error) {
          console.log(error);
        });

  },
  showAll: function () {

    this.setData({
      services: this.data.services.length > 8 ? this.data.minServices : this.data.allServices
    });
    console.log("showAll...", this.data.services);
  },
  // 倒计时(正在秒杀)
  theTime: function (end1) {
    var totalSecond = end1- Date.parse(new Date()) / 1000;

    var interval = setInterval(function () {
      // 秒数
      var second = totalSecond;
      // 小时位
      var hr = Math.floor(second / 3600);
      var hrStr = hr.toString();
      if (hrStr.length == 1) hrStr = '0' + hrStr;
      // 分钟位
      var min = Math.floor((second - hr * 3600) / 60);
      var minStr = min.toString();
      if (minStr.length == 1) minStr = '0' + minStr;
      // 秒位
      var sec = second - hr * 3600 - min * 60;
      var secStr = sec.toString();
      if (secStr.length == 1) secStr = '0' + secStr;

      this.setData({
        countDownHour: hrStr,
        countDownMinute: minStr,
        countDownSecond: secStr,
      });
      totalSecond--;
      if (totalSecond < 0) {
        clearInterval(interval);
        console.log('活动已结束')
        this.setData({
          countDownHour: '00',
          countDownMinute: '00',
          countDownSecond: '00',
          miaoTime:3
        });
      }
    }.bind(this), 1000);
  },
  // 倒计时(即将开始)
  theTime1: function (start1) {
    var totalSecond = start1 - Date.parse(new Date()) / 1000;

      var interval = setInterval(function () {
      // 秒数
      var second = totalSecond;
      // 小时位
      var hr = Math.floor(second / 3600);
      var hrStr = hr.toString();
      if (hrStr.length == 1) hrStr = '0' + hrStr;
      // 分钟位
      var min = Math.floor((second - hr * 3600) / 60);
      var minStr = min.toString();
      if (minStr.length == 1) minStr = '0' + minStr;
      // 秒位
      var sec = second - hr * 3600 - min * 60;
      var secStr = sec.toString();
      if (secStr.length == 1) secStr = '0' + secStr;

      this.setData({
        countDownHour: hrStr,
        countDownMinute: minStr,
        countDownSecond: secStr,
      });
      totalSecond--;
      if (totalSecond < 0) {
        clearInterval(interval);
        console.log('活动已开始')
        this.setData({
          countDownHour: '00',
          countDownMinute: '00',
          countDownSecond: '00',
          miaoTime: 1
        });
      }
    }.bind(this), 1000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: options.merchantName,
    })
console.log(options)
    this.setData({
      miaoTime: options.miaoTime,
    })
if(options.times=='12'){
    if (this.data.miaoTime == 1) {
      // var starttime = Date.parse(new Date()) / 1000;
      // console.log(starttime)
      var endtime = options.enddate;
      console.log(endtime)
      this.theTime(endtime)
    } else if (this.data.miaoTime == 2) {
      var starttime = options.startdate;
      console.log(starttime)
      this.theTime1(starttime)
    }
}
    console.log(options);
    const userId = wx.getStorageSync('userId');
    this.setData({
      id: options.id,
      userId: userId,
      miaoTime: options.miaoTime,
       times: options.times
    });

    this.loadData();

    if (options.qFlag) {
      if (options.id == "9976161") {
        this.setData({
          gonlList: [{
            url: "http://logo.denong.com/Fo5sLJvQc9JUSDvIZ7c7ZWJuOJS_", material_name: "怡庭 Bistrot B", id: "9976161", price: "30", fen: "10", orangePrice: "53", name: "黑椒牛柳"
          }]
        });

      }
      if (options.id == "9976162") {
        this.setData({
          gonlList: [{ url: "http://logo.denong.com/Fo5sLJvQc9JUSDvIZ7c7ZWJuOJS_", material_name: "山海楼(德胜门店)", id: "9976162", price: "50", fen: "40", orangePrice: "66", name: "黑椒牛排" }]
        });

      }

    }

  }
})
